package com.niit.service;

import com.niit.dao.UsersDao;
import com.niit.entity.Users;

import java.sql.SQLException;
import java.util.List;
import java.util.regex.Pattern;

public class UsersService {
    UsersDao usersDao = new UsersDao();

    //    用于验证登录参数
    public boolean login(String phoneNum, String password) throws SQLException {
        if (phoneNum == null || password == null || phoneNum.equals("") || password.equals("")) {
            System.out.println("用户名或密码不能为空！");
            return false;
        }
        Boolean login = usersDao.login(phoneNum, password);
        if (login) {
            System.out.println("登录成功!");
            return true;
        } else
            System.out.println("登录失败!");
            return false;

    }

    public boolean register(String phoneNum, String password){
        if (phoneNum == null || password == null || phoneNum.equals("") || password.equals("")) {
            System.out.println("用户名或密码不能为空！");
            return false;
        }
//        判断用户名是否符合手机号的规则
        if(!Pattern.matches("^1[3-9]\\d{9}$",phoneNum)){
            System.out.println("手机号输入错误！");
            return false;
        }
//        判断密码长度是否大于6个字符
        if(password.length()<6){
            System.out.println("密码长度不能小于6个字符");
            return false;
        }
        Users users = new Users();
        users.setPhoneNum(phoneNum);
        users.setPassword(password);
        users.setRole(1);
        Boolean aBoolean = usersDao.addUser(users);
        System.out.println("注册成功");
        return aBoolean;
    }

    public List<Users> listUsers(String phoneNum,Integer gender,Integer role, int page, int pageSize) throws SQLException {
        return usersDao.listUsers(phoneNum,gender,role,page,pageSize);
    }
}
